{{ extend '../_layout/layout.html' }}

{{ block 'header'}}
<link rel="stylesheet" href="/public/css/profile.css">
{{ /block }}

{{ block 'content' }}
<div class="container clearfix">
    <ul class="side_nav">
        <li class="disabled">用户设置</li>
        <li><a href="/settings/profile" class="nav_active">基本信息</a></li>
        <li><a href="/settings/admin">账户设置</a></li>
    </ul>
    <div class="content">
        <form id="profile_form">
            <label for="email">账号</label>
            <p>{{ user.email }}</p>
            <label for="nickname">昵称</label>
            <input type="text" name="nickname" id="nickname" value="{{ user.nickname }}">
            <label for="bio">介绍</label>
            <textarea name="bio" id="bio" cols="30" rows="10"></textarea>
            <label>性别</label>
            {{ if user.gender == 1 }}
            <input type="radio" name="gender" value="0">男
            <input type="radio" name="gender" value="1" checked="checked">女
            <input type="radio" name="gender" value="-1">保密
            {{ else if user.gender == -1 }}
            <input type="radio" name="gender" value="0">男
            <input type="radio" name="gender" value="1">女
            <input type="radio" name="gender" value="-1" checked="checked">保密
            {{ else }}
            <input type="radio" name="gender" value="0" checked="checked">男
            <input type="radio" name="gender" value="1">女
            <input type="radio" name="gender" value="-1">保密
            {{ /if }}
            <label for="birthday">生日</label>
            <input type="date" name="birthday" id="birthday" value="2021-10-13">
            <div class="avatar">
                <label>头像</label>
                <img src="/public/img/user.jpeg" alt="头像">
                <button type="button" class="btn">点击更换头像</button>
            </div>
            <button class="btn">保存</button>
        </form>
    </div>
</div>
{{ /block }}

{{ block 'script'}}
<script>
    // 为表单绑定一个submit事件
    $('#profile_form').on('submit', function (e) {
        // 阻止表单的默认事件
        e.preventDefault()
        let formData = $(this).serialize()
        $.ajax({
            url: '/settings/profile',
            type: 'post',
            data: formData,
            dataType: 'json',
            success: function (data) {
                if (data.err_code === 0) {
                    // 提示修改成功，并刷新页面
                    window.alert('用户信息修改成功')
                    window.location.href = '/settings/profile'
                }else {
                    window.alert(data.msg)
                }
            }
        })

    })
    // 给介绍的多文本赋值
    $('#bio').val('{{ user.bio }}')
</script>
{{ /block }}